package com.samshow.messageConverter;

import com.samshow.baseVo.DefaultResult;
import com.samshow.baseVo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * Created by niwb on 2017/3/7.
 */
public class BaseMessageConverter extends MappingJackson2HttpMessageConverter {
    @Autowired
    Environment environment;
    private Logger log = LoggerFactory.getLogger(this.getClass());


    @Override
    protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
       if (environment.getProperty("imessage.enable",Boolean.class)){
           //log.info("writeInternal-imessage.enable:true");
           Result result = new DefaultResult(object);
           super.writeInternal(result, type, outputMessage);
       }else {
           //log.info("writeInternal-imessage.enable:false");
           super.writeInternal(object, type, outputMessage);
       }

    }


}
